<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<script type="text/javascript" language="javascript">
    var potonganStoreGroup= createStoreGroup(false, 'mGpotongansiswa', ['id_potongan','nis','nama','kelas','th_ajaran_mulai','thbl_mulai','thbl_selesai','kd_biaya','biaya','potongan','keterangan','tgl_entri','id_user','fgroup'],
    'fgroup', '<?php echo base_url(); ?>' + 'potongan/get_rows');
    var combothajaranStorePot = createStore(false, 'mcombothajaranStorePot',['id_th_ajaran','tahun_ajaran'],'<?php echo base_url(); ?>' + 'tahunajaran/get_rows_combo');
    var combobiayaStorePot = createStore(false, 'mcombobiayaStorePot',['kd_biaya','keterangan','biaya_dasar'],'<?php echo base_url(); ?>' + 'masterbiaya/get_rows_combo');
    //===================one row edit
    var twinsiswaStorePot = createStore(false, 'mtwinsiswaStorePot',['nis','nama','kd_kelas','kelas'],'<?php echo base_url(); ?>' + 'datasiswa/get_rows_twin');
    var allsiswaStorePot = createStore(false, 'mallsiswaStorePot',['nis','nama','kd_kelas','kelas'],'<?php echo base_url(); ?>' + 'datasiswa/get_rows_all');
    var combo_bulanmulai_Store=createArrayStore2( [{name:'num'}, {name:'name'}],bulan_data);
    var combo_bulanselesai_Store=createArrayStore2( ['num', 'name'],bulan_data);
    defineTwinSiswa_windows('Windowfindsiswapot', 'Windowfindsiswapot_id', twinsiswaStorePot, function(){			
        var sm = this.getSelectionModel();
        var sel = sm.getSelection();
        //                console.log('doublekil');
        if (sel.length > 0) {	
            Ext.getCmp('Pot_nis').setValue(sel[0].get('nis'));
            Ext.getCmp('Pot_nama').setValue(sel[0].get('nama'));
            Ext.getCmp('Pot_kelas').setValue(sel[0].get('kelas'));                    
            Ext.getCmp('Windowfindsiswapot_id').close();
            Ext.getCmp('Pot_th_ajaran').focus();
        }
    });
    function set_store_bulan(minMonth){//       
        
//                                    var minMonth=new Date().getMonth();
                                    var recInsert=0;
                                    var data = [];
                                    Ext.Array.forEach(Ext.Date.monthNames, function(name, i) {
                                            
                                        var m=i+1;
                                        if (m==minMonth+1 || m>minMonth+1  )    
                                        {
                                            data[recInsert] = {name: name, num: m};
                                            recInsert++;
                                        }
                                        
                                    });
                                    return data;
                                
    }
    
    Ext.define('FormPot', {
        extend          : 'Ext.form.Panel',
        alias           : 'widget.PotForm',
        id              : 'FormPot_id',
        requires        : [
            'Ext.form.Field'
        ],
        defaultType     : 'textfield',
        defaults        : {
            allowBlank: false,
            labelWidth: 120,
            labelAlign: 'right',
            anchor:'90%'
            
        },
        monitorValid: true,
        url: '<?php echo base_url(); ?>' + 'potongan/update_rows',
        buttonAlign     : 'center',
        padding         : 5,
        style           : 'background-color: #fff;',
        border          : false,
        initComponent   : function(){
            this.items = [    
                {
                    xtype: 'textfield'                            
                    ,name: 'id_potongan'                            
                    ,fieldLabel: 'Rec.ID',
                    id:'Pot_id',                                    
                    emptyText: 'id',
                    allowBlank:true
                    ,readOnly:true                                    
                }      
                ,
                {
                    xtype: 'twincombo'
                    ,name: 'nis'
                    ,fieldLabel: 'Nis',
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    id:'Pot_nis',
                    store:twinsiswaStorePot,
                    menu:'Windowfindsiswapot',
                    enableKeyEvents:true,
                    allowBlank:false
                    ,listeners:{
                        keyup: function(f,e,opt) { 
                            if (e.getKey()==113){
                                f.onTrigger1Click();
                            }          
                            if (e.getKey()==13){
                                if(f.getValue().length>0){
                                    var rec=  findNis(f.getValue(), allsiswaStorePot);
                                    if(rec){
                                      
                                        Ext.getCmp('Pot_nama').setValue(rec.get('nama'));
                                        Ext.getCmp('Pot_kelas').setValue(rec.get('kelas')); 
                                        Ext.getCmp('Pot_th_ajaran').focus();
                                    }else{
                                        set_message(2,'Data Siswa Tidak Terdaftar !!!');
                                        f.setValue('');
                                    }
                                };
                            }
                        }
                    }
                    //                    ,readOnly:true                                    
                }      
                ,{
                    name: 'nama',
                    id: 'Pot_nama',
                    fieldLabel: 'Nama Siswa',
                    maxLength:50,
                    emptyText: 'Nama Siswa',
                    allowBlank: true
                    ,readOnly:true                    
                },{
                    name: 'kelas',
                    id: 'Pot_kelas',
                    fieldLabel: 'Kelas',
                    maxLength:50,
                    emptyText: 'Kelas',
                    allowBlank: true
                    ,readOnly:true
                },{xtype: 'combo',                                                            
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    fieldLabel: 'Biaya', 
                    name:'kd_biaya',
                    //                                    fieldLabel: 'Jenis Biaya',                        
                    id: 'Pot_kd_biaya',
                    mode:'local',
                    store: combobiayaStorePot,
                    valueField: 'kd_biaya',
                    displayField: 'keterangan',
                    typeAhead: true,
                    triggerAction: 'all',                    
                    allowBlank: false,
                    editable: false
                }
                ,{
                    xtype:'numericfield',                                
                    fieldLabel:'Potongan',
                    name:'potongan',
                    id: 'Pot_potongan',
                    allowBlank: false,                                                                        
                    hideTrigger: true,
                    fieldStyle: 'text-align: right;',
                    minValue: 0                                                                                         
                    ,
                    //                    readOnly:true,
                    currencySymbol: null,
                    useThousandSeparator: true,
                    thousandSeparator: ',',
                    alwaysDisplayDecimals: false
                    ,tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css
                },{
                    xtype:'combo',
                    afterLabelTextTpl: required_css,
                    name: 'th_ajaran_mulai',
                    id: 'Pot_th_ajaran_mulai',
                    fieldLabel: 'Tahun Ajaran Mulai',                                    
                    //                    labelWidth: 80,
                    emptyText: 'Tahun Ajaran Mulai'
                    ,mode:'local',
                    store: combothajaranStorePot,
                    valueField: 'tahun_ajaran',
                    displayField: 'tahun_ajaran',
                    typeAhead: true,
                    triggerAction: 'all',                    
                    allowBlank: false,
                    editable: false  ,
                    hiddenName:'th_ajaran_mulai'
                    ,listeners:{
                        select:function(cmb,rec){
                            console.log(rec[0].get('tahun_ajaran'));
                        }
                    }
                }
                ,{
                    xtype:'fieldcontainer',
                    fieldLabel:'ThBl Mulai',
                    //                    labelWidth: 130,
                    layout: 'hbox',                    
                    defaultType: 'textfield',                    
                    //                    margin: '0 0 5 0',
                    defaults: {
                        labelWidth: 80,
                        labelAlign: 'right'
                        ,hideLabel: 'true'
                    },
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    items: [
                        {
                            xtype:'numberfield',
                            name:'tahun_mulai',
                            fieldLabel:'Tahun Mulai',
                            id: 'Pot_tahun_mulai',
                            emptyText:'Tahun Mulai',
                            allowBlank: false,                                                                        
                            //                            hideTrigger: true,
                            fieldStyle: 'text-align: right;',
                            minValue: new Date().getFullYear()
                            ,flex:1
                        },
                        {
                            xtype: 'combobox',
                            name: 'bulan_mulai',
                            id: 'Pot_bulan_mulai',
                            displayField: 'name',
                            valueField: 'num',
                            queryMode: 'local',
                            emptyText: 'Bulan Mulai',
                            hiddenName:'bulan_mulai',
                            store:combo_bulanmulai_Store,
                            //                            margins: '0 6 0 0',
//                            store: new Ext.data.Store({
//                                fields: ['name', 'num'],
//                                data: (function() {
//                                    var minMonth=new Date().getMonth();
//                                    var recInsert=0;
//                                    var data = [];
//                                    Ext.Array.forEach(Ext.Date.monthNames, function(name, i) {
//                                            
//                                        var m=i+1;
//                                        if (m==minMonth+1 || m>minMonth+1  )    
//                                        {
//                                            data[recInsert] = {name: name, num: m};
//                                            recInsert++;
//                                        }
//                                        
//                                    });
//                                    return data;
//                                })()
//                            }),
                            width: 100,
                            allowBlank: false,
                            forceSelection: true
                            ,flex:1
                        }
                    ]
                }
                ,{
                    xtype:'fieldcontainer',
                    fieldLabel:'ThBl Selesai',
                    //                    labelWidth: 130,
                    layout: 'hbox',                    
                    defaultType: 'textfield',                    
                    //                    margin: '0 0 5 0',
                    defaults: {
                        labelWidth: 80,
                        labelAlign: 'right'
                        ,hideLabel: 'true'
                    },
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    items: [
                        {
                            xtype:'numberfield',
                            name:'tahun_selesai',
                            fieldLabel:'Tahun Selesai',
                            id: 'Pot_tahun_selesai',
                            emptyText:'Tahun Selesai',
                            allowBlank: false,                                                                        
                            //                            hideTrigger: true,
                            fieldStyle: 'text-align: right;',
                            minValue: new Date().getFullYear()
                            ,flex:1
                        },
                        {
                            xtype: 'combobox',
                            name: 'bulan_selesai',
                            id: 'Pot_bulan_selesai',
                            displayField: 'name',
                            valueField: 'num',
                            queryMode: 'local',
                            emptyText: 'Bulan Selesai',
                            hiddenName:'bulan_selesai',
                            //                            margins: '0 6 0 0',
                            store: combo_bulanselesai_Store,
                            width: 100,
                            allowBlank: false,
                            forceSelection: true
                            ,flex:1
                        }
                    ]
                },{
                    name: 'keterangan',
                    id: 'Pot_keterangan',
                    fieldLabel: 'Keterangan',
                    maxLength:50,
                    emptyText: 'Keterangan',
                    allowBlank: false
                    ,tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css
                    //                    ,readOnly:true                   
                }
                                
                    
                
            ];
            this.buttons = [
                {
                    text: 'Simpan',                    
                    itemId: 'simpan',
                    id:'btn_Pot_simpan',
                    iconCls: 'icons-add',
                    formBind: true,
                    scope: this,
                    handler: this.submit
                },
                {
                    text: 'Batal',
                    action: 'cancel',
                    itemId: 'batal',
                    iconCls: 'cancel',
                    handler: function(){
                        this.up('window').close();
                    }
                }
            ];
            this.callParent(arguments);
        },
        submit: function(){
            var parcmd=Ext.getCmp('btn_Pot_simpan').getText();
            var mpotongan=0;
            if(parcmd === 'Simpan'){
                parcmd='insert';
            }else if(parcmd === 'Edit'){
                parcmd='update';
               
            }
            mpotongan=Ext.getCmp('Pot_potongan').getValue();
            var thblmulai=setThbl(Ext.getCmp('Pot_tahun_mulai').getValue(), Ext.getCmp('Pot_bulan_mulai').getValue());
            var thblselesai=setThbl(Ext.getCmp('Pot_tahun_selesai').getValue(), Ext.getCmp('Pot_bulan_selesai').getValue());
            if(parseInt(thblmulai)>parseInt(thblselesai)){
                set_message(2, 'ThBl Mulai Lebih Besar Dari ThBl Selesai !!!');
                return;
            }
            
            Ext.getCmp('FormPot_id').getForm().submit({
                url: this.url,
                scope: this,                               
                params: {
                    cmd: parcmd,
                    mpotongan:mpotongan,
                    thbl_mulai:thblmulai,
                    thbl_selesai:thblselesai
                },
                waitMsg: 'Saving Data...',
                success: function(form, action) {                    
                    set_message(0,action.result.msg);                    
                    Ext.getCmp('gridpotongan').store.reload(); 
                    Ext.getCmp('FormPot_id').getForm().reset();
                    Ext.getCmp('WindowPot_id').close();
                },
                failure: function(form, action) {
                    if(action.result.msg=='Session Expired') {
                        session_expired(action.result.msg);
                    }else{
                        set_message(1, action.result.msg);
                    }
                                            
                    
                }
            });
        },
        showError: function(msg, title){
            title = title || 'Error';
            Ext.Msg.show({
                title: title,
                msg: msg,
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK,
                fn: function(btn){
                    if (btn == 'ok' && msg == 'Session Expired') {
                        window.location = '<?= site_url("auth/login") ?>';
                    }
                }
            });
        }
    });
    
    
    
    Ext.define('WindowPot', {
        extend          : 'Ext.window.Window',
        title           : 'Form Potongan',
        width           : 450,
        height          : 350,
        layout          : 'fit',
        autoShow        : true,
        modal           : true,
        alias           : 'widget.potEdit',
        id              : 'WindowPot_id',
        initComponent   : function(){
            this.items = [
                Ext.widget('PotForm')
            ];
            this.callParent(arguments);
        }

    });
    
    
    
    defineTabForm(
    {
        name:'MyTabPotonganBiayaSiswa',
        xtype:'TabPotonganBiayaSiswa',
        alias:'widget.TabPotonganBiayaSiswa',
        title:'Potongan Biaya Siswa',
        id:'tab10'
    },
    {
        id:'panelpotongan',
        items:[
            {
                xtype:'grid',
                id:'gridpotongan',
                stateful:true,
                stateId:'stateGridpotongan',
                store: potonganStoreGroup,//Ext.data.StoreManager.lookup('mMasterAccountStore'),
                stripeRows: true,
                loadMask: true
                //                        ,plugins: [Ext.create('Ext.ux.grid.plugin.GroupingPanel')]
                //                        ,                        
                //                        sm:sm_masteraccount,
                ,columns:[
                    {
                        header: 'Edit/Delete',
                        menuDisabled: true,
                        sortable: false,
                        xtype: 'actioncolumn',
                        width: 70,
                        align:'center',
                        items: [
                            {
                                iconCls: 'icon-edit-record',
                                tooltip: 'Edit Row',
                                handler: function(grid, rowIndex, colIndex) {
                                    var rec = grid.getStore().getAt(rowIndex);
                                    var winPot=Ext.create('WindowPot');
                                    winPot.setTitle('Edit Form');
                                    
                                    allsiswaStorePot.load();
                                    
                                    
                                    Ext.getCmp('Pot_kd_biaya').getStore().load();
                                    Ext.getCmp('btn_Pot_simpan').setText('Edit');
                                    Ext.getCmp('btn_Pot_simpan').setIconCls('icon-edit-record');
                                    Ext.getCmp('Pot_id').setReadOnly(true);
                                    Ext.getCmp('Pot_id').setFieldStyle('readonly-input');
                                    Ext.getCmp('Pot_nis').setReadOnly(true);
                                    Ext.getCmp('Pot_nis').setFieldStyle('readonly-input');
                                    Ext.getCmp('Pot_nama').setReadOnly(true);
                                    Ext.getCmp('Pot_nama').setFieldStyle('readonly-input');
                                    Ext.getCmp('Pot_kelas').setReadOnly(true);
                                    Ext.getCmp('Pot_kelas').setFieldStyle('readonly-input');
                                                                      
                                    Ext.getCmp('Pot_th_ajaran_mulai').setValue(rec.get('th_ajaran_mulai'));
                                    Ext.getCmp('Pot_id').setValue(rec.get('id_potongan')); 
                                    Ext.getCmp('Pot_nis').setValue(rec.get('nis')); 
                                    Ext.getCmp('Pot_nama').setValue(rec.get('nama')); 
                                    Ext.getCmp('Pot_kelas').setValue(rec.get('kelas')); 
                                    Ext.getCmp('Pot_kd_biaya').setValue(rec.get('kd_biaya')); 
                                    Ext.getCmp('Pot_potongan').setValue(rec.get('potongan')); 
                                    
                                    var thbl_mulai=rec.get('thbl_mulai');
                                    var thbl_selesai=rec.get('thbl_selesai');
                                    
                                    
                                    Ext.getCmp('Pot_tahun_mulai').setValue(thbl_mulai.substring(0,4)); 
                                    Ext.getCmp('Pot_bulan_mulai').setValue(parseInt(thbl_mulai.substring(4,6))); 
                                    Ext.getCmp('Pot_tahun_selesai').setValue(thbl_selesai.substring(0,4)); 
                                    Ext.getCmp('Pot_bulan_selesai').setValue(parseInt(thbl_selesai.substring(4,6))); 
                                    Ext.getCmp('Pot_keterangan').setValue(rec.get('keterangan'));
                                                                                                                                                                     
                                    winPot.show();
                                }
                            },{
                                getClass: function(v, meta, rec) {
                                    return 'icon-delete';
                                },
                                getTip: function(v, meta, rec) {
                                    return 'Delete Plant';
                                },
                                handler: function(grid, rowIndex, colIndex) {
                                    var rec = grid.getStore().getAt(rowIndex);
                                    console.log(rec.data);
                                    //                                            Ext.Msg.alert('Delete', 'Delete ' + rec.get('rekening'));
                                    Ext.Msg.show({
                                        title: 'Confirm',
                                        msg: 'Are you sure delete selected row ?',
                                        buttons: Ext.Msg.YESNO,
                                        icon: Ext.Msg.QUESTION,
                                        fn: function(btn){
                                            if (btn == 'yes') {
                                                        
                                                var data = '';
                                                data = Ext.JSON.encode(rec.data);
                                                            
                                                Ext.Ajax.request({                                                            
                                                    url: '<?php echo base_url(); ?>' +'potongan/delete_row',
                                                    method: 'POST',
                                                    params: {
                                                        cmd: 'delete',
                                                        postdata: data
                                                    },
                                                    success: function(obj) {
                                                        var   resp = Ext.decode(obj.responseText);                                                                
                                                        if(resp.success==true){
                                                            //                                                                    Ext.Msg.alert('info',resp.msg);
                                                            set_message(0,resp.msg);
                                                            Ext.getCmp('gridpotongan').store.reload();                                                                                                                                     
                                                        }else{
                                                            Ext.Msg.show({
                                                                title: 'Error',
                                                                msg: resp.msg,
                                                                modal: true,
                                                                icon: Ext.Msg.ERROR,
                                                                buttons: Ext.Msg.OK,
                                                                fn: function(btn){
                                                                    if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                                        window.location = '<?= site_url("auth/login") ?>';
                                                                    }
                                                                }
                                                            });
                                                        }
                                                    },
                                                    failure: function(obj) {
                                                        var  resp = Ext.decode(obj.responseText);
                                                        set_message(0, resp.reason);
                                                        //                                                                Ext.Msg.alert('info',resp.reason);
                                                    }                                                            
                                                });                 
                                            } 
                                        }
                                    });
                                }
                            }
                        ]
                    }
                           
                    ,
                            
                            
                    {
                        header: "NIS",
                        dataIndex: 'nis',
                        sortable: true,
                        width: 100,
                        //                                groupable:      true,groupHeaderTpl: '{name}',
                        align:'center'
                        ,hidden:true
                                
                    }
                    ,{
                        header: "Nama Siswa",
                        dataIndex: 'nama',
                        sortable: true,
                        width: 120      
                        ,hidden:true
                    }
                    ,{
                        header: "kd_kelas",
                        dataIndex: 'kd_kelas',
                        sortable: true,
                        width: 50     
                        ,align:'center'
                        ,hidden:true
                    }
                    ,{
                        header: "Kelas",
                        dataIndex: 'kelas',
                        sortable: true,
                        width: 80     
                        ,align:'center'
                        ,hidden:true
                    }                    
                    ,{
                        header: "Kode Biaya",
                        dataIndex: 'kd_biaya',
                        sortable: true,
                        width: 80     
                        ,align:'center'
                        //                                ,hidden:true
                    }
                    ,{
                        header: "Biaya",
                        dataIndex: 'biaya',
                        sortable: true,
                        width: 120     
                        //                                ,align:'center'
                    }
                    ,{
                        xtype:'numbercolumn',
                        header: "Potongan",
                        dataIndex: 'potongan',
                        sortable: true,
                        width: 100     
                        ,align:'right'
                        ,format:'0,0'
                    },{
                        header: "Keterangan",
                        dataIndex: 'keterangan',
                        sortable: true,
                        width: 120     
                        //                                ,align:'center'
                    }
                    ,{
                        header: "Tahun Ajaran Mulai",
                        dataIndex: 'th_ajaran_mulai',
                        sortable: true,
                        width: 120     
                        ,align:'center'
                        //                        ,hidden:true
                    }
                    ,{
                        //                        xtype:'numbercolumn',
                        header: "ThBl Mulai",
                        dataIndex: 'thbl_mulai',
                        sortable: true,
                        width: 100     
                        ,align:'center'
                        
                    },{
                        //                        xtype:'numbercolumn',
                        header: "ThBl Selesai",
                        dataIndex: 'thbl_selesai',
                        sortable: true,
                        width: 100     
                        ,align:'center'
                        
                    }
                    ,{
                        header: "Tanggal Entry",
                        dataIndex: 'tgl_entri',
                        sortable: true,
                        width: 120     
                        ,align:'center'
                                
                    }
                    ,{
                        header: "User Entry",
                        dataIndex: 'id_user',
                        sortable: true,
                        width: 80     
                        ,align:'left'
                                
                    }
                ]
                ,tbar:{
                    xtype: 'toolbar',
                    padding:'2 0 2 5',
                    items: [
                        {
                            xtype: 'button',
                            text: 'Add',
                            iconCls: 'icons-add',
                            onClick: function()
                            {
                                
                                
                                var winpot=Ext.create('WindowPot');
                                winpot.setTitle('Add Form');
                                twinsiswaStorePot.load();
//                                var strblnmulai=set_store_bulan(0);
//                                combo_bulanmulai_Store.loadData(strblnmulai);
//                                var strblnselesai=set_store_bulan(0);
//                                combo_bulanselesai_Store.loadData(strblnselesai);
                                Ext.getCmp('btn_Pot_simpan').setText('Simpan');
                                Ext.getCmp('btn_Pot_simpan').setIconCls('icons-add');
                                Ext.getCmp('Pot_id').setVisible(false);
                                
                                
                                
                                //                                setDefaultStoreProxy(KelompokAccountStore, urlKelompok);
                                winpot.show();
                            } 
                        },
                        '-',
                        {
                            width: 300,
                            //                                fieldLabel: 'Search',
                            //                                labelWidth: 50,
                            xtype: 'searchfield',
                            store: potonganStoreGroup,
                            emptyText: 'Quick Search...'
                        }
                    ]
                }
                ,bbar:{                        
                    xtype: 'pagingtoolbar',
                    store: potonganStoreGroup,
                    pageSize: ENDPAGE,
                    displayInfo: true
                    //                    plugins: Ext.create('Ext.ux.SlidingPager', {})
                }
                        
                ,features:[ {
                        ftype: 'grouping',
                        //                                groupHeaderTpl: '{columnName}: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})',
                        groupHeaderTpl: '{name}',
                        hideGroupedHeader: true,
                        startCollapsed: false,
                        id: 'pot_grid_Grouping'
                    }]
            }
        ]
    },
    function(){
        var storegrid=Ext.getCmp('gridpotongan').store;                
        storegrid.loadPage(1);
        allsiswaStorePot.load();
//        var str='201401';
//        console.log(str.substring(1,2));
//        var strblnmulai=set_store_bulan(0);
//                                    combo_bulanmulai_Store.loadData(strblnmulai);
//                                    var strblnselesai=set_store_bulan(0);
//                                    combo_bulanselesai_Store.loadData(strblnselesai);
        //        var arr={test:'testiing'};
        //        console.log(arr.test);
    }
);
</script>